package com.xiaouyudeguang.common.utils;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

public class RestTemplateUtils {

    private static final RestTemplate restTemplate = new RestTemplate();

    public static <T> T getForObject(String url, Class<T> cls, Object... uriVariables) {
        return restTemplate.getForObject(url, cls, uriVariables);
    }

    public static <T> T postForObject(String url, Object obj, Class<T> cls, Object... uriVariables) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Object> httpEntity = new HttpEntity<>(obj, headers);
        return postForObject(url, httpEntity, cls, uriVariables);
    }

    public static <T> T postForObject(String url, HttpEntity<Object> httpEntity, Class<T> cls, Object... uriVariables) {
        return restTemplate.postForObject(url, httpEntity, cls, uriVariables);
    }
}
